<template>
  <div>
    <a-input v-model="dictOption.dictName" :placeholder="$t('m.common.label.option_select')" @click="selectDict" readOnly>
      <ych-icon slot="addonAfter" type="setting"/>
    </a-input>
    <a-button v-if="canEdit" icon="ordered-list" @click="openEditDictDialog">{{$t('m.common.label.Maintain_data')}}</a-button>
    <select-dict-dialog ref="selectDictDialog" @selectFinished="selectFinished" :groupId="groupId"></select-dict-dialog>

    <edit-dict-dialog v-if="canEdit" ref="editDictDialog" :group-id="groupId"></edit-dict-dialog>
  </div>
</template>

<script>

  import SelectDictDialog from '../../select/dialog/SelectDictDialog';
  import EditDictDialog from "../../select/dialog/EditDictDialog";

  export default {
    name: "DictSetting",
    props: {
      dictOption: Object,
      groupId: {
        type: String,
        required: true
      },
      canEdit: {
        type: Boolean,
        default: false
      },
    },
    components: {
      EditDictDialog,
      SelectDictDialog
    },
    data() {
      return {}
    },
    methods: {
      openEditDictDialog(){
        this.$refs.editDictDialog.visible = true
      },
      selectDict() {
        this.$refs.selectDictDialog.radio = true;
        this.$refs.selectDictDialog.visible = true;
      },
      selectFinished(selectedRowKeys, selectionRows) {
        let dictOption;
        if (selectionRows) {
          dictOption = {
            dictCode: selectionRows[0].dictCode,
            dictName: selectionRows[0].dictName
          }
        } else {
          dictOption = {}
        }
        this.$emit("input", dictOption);
      }
    }
  }
</script>
